package com.work.customer.controller;

import com.work.customer.pojo.Menu;
import com.work.customer.service.MenuService;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    //查询角色所拥有的全部权限
    @GetMapping("/findById")
    public Result find(@RequestParam("id") Integer rid){
        try {
            List<Menu> menus = menuService.find(rid);
            return new Result(true, StatusCode.FIND_Ok,"查询成功",menus);
        }catch (Exception e){
            return new Result(true, StatusCode.FIND_ERROR,"查询失败");
        }

    }

    //给角色添加权限
    @PostMapping("/add")
    public Result add(@RequestParam("id") Integer rid,Integer[] mids){
        try {
            menuService.add(rid,mids);
            return new Result(true, StatusCode.ADD_OK,"添加成功");
        }catch (Exception e){
            return new Result(true, StatusCode.ADD_ERROR,"添加失败");
        }
    }

    //给角色删除权限
    @DeleteMapping("/delete")
    public Result delete(@RequestParam("id") Integer rid,Integer[] mids){
        try {
            menuService.delete(rid,mids);
            return new Result(true, StatusCode.DELETE_OK,"删除成功");
        }catch (Exception e){
            return new Result(true, StatusCode.DELETE_ERROR,"删除失败");
        }
    }

    //给角色修改权限
    @PutMapping("/update")
    public Result update(@RequestParam("id") Integer rid,Integer[] mids){
        try {
            menuService.update(rid,mids);
            return new Result(true, StatusCode.UPDATE_Ok,"修改成功");
        }catch (Exception e){
            return new Result(true, StatusCode.UPDATE_ERROR,"修改失败");
        }
    }
}
